﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

namespace TomMaton.Umbraco.AmazonS3
{
    public class amazonS3Functionality
    {

        public static string pluginConfigPath = HttpContext.Current.Server.MapPath("/config/AmazonS3.config").ToString();
        private static string myAWSAccessKeyId = getKey("AccessKeyId", pluginConfigPath).ToString();
        private static string mySecretAccesKeyId = getKey("SecretAccesKeyId", pluginConfigPath).ToString();


        protected static string showContents(string bucketName)
        {
            com.amazonaws.s3.doc.AmazonS3 myS3 = new com.amazonaws.s3.doc.AmazonS3();
            DateTime myTime = DateTime.Now;

            string mySignature = S3Helper.GetSignature(mySecretAccesKeyId, "ListBucket", myTime);
            string bucketContentList = string.Empty;

            com.amazonaws.s3.doc.ListBucketResult myResults = myS3.ListBucket(bucketName.ToString(),
                "",
                "",
                0,
                false,
                "|",
                myAWSAccessKeyId,
                S3Helper.GetTimeStamp(myTime),
                true,
                mySignature,
                null);

            //Iterate through bucket contents
            if (myResults.Contents != null)
            {
                //Literal bucketContentContainer = new Literal();
                bucketContentList = "<ul style=\"list-style:none;margin:0;padding:0;\">";

                foreach (com.amazonaws.s3.doc.ListEntry myBucketItem in myResults.Contents)
                {
                    bucketContentList += String.Format("<li style=\"list-style:none;margin:5px 0 5px 5px;padding:0;display:block;\"><a href=\"#\" onClick=\"selectFile('{0}')\">{1}</a></li>", String.Format("https://s3.amazonaws.com/{0}/{1}", bucketName, myBucketItem.Key), myBucketItem.Key);
                }
                bucketContentList += "</ul>";
            }
            return bucketContentList;

        }

        public static string generateBucketList()
        {
            Literal _bucketListContainer = new Literal(); ;

            com.amazonaws.s3.doc.AmazonS3 myS3 = new com.amazonaws.s3.doc.AmazonS3();
            DateTime myTime = DateTime.Now;

            //List All buckets under this user
            com.amazonaws.s3.doc.ListAllMyBucketsResult myBuckets = myS3.ListAllMyBuckets(myAWSAccessKeyId,
                S3Helper.GetTimeStamp(myTime),
                true,
                S3Helper.GetSignature(mySecretAccesKeyId, "ListAllMyBuckets", myTime));

            string listedBuckets = "";

            foreach (com.amazonaws.s3.doc.ListAllMyBucketsEntry bucketEntry in myBuckets.Buckets)
            {
                listedBuckets += String.Format("<div style=\"border:1px solid #cecece;margin-bottom:10px;\"><div class=\"menubar\" ><span style=\"padding:5px 0 0 5px;margin-bottom:-5px;\">Folder name: {0}</span></div>", bucketEntry.Name.ToString());
                listedBuckets += showContents(bucketEntry.Name.ToString());
                listedBuckets += "</div>";
            }

            return listedBuckets;


        }

        public static string getKey(string typeOfAccessKey, string pluginConfigPath)
        {
            XmlDocument amazonS3Config = new XmlDocument();
            amazonS3Config.Load(pluginConfigPath);

            XmlNode getNode = amazonS3Config.SelectSingleNode("/AmazonS3");

            return getNode[typeOfAccessKey].InnerText.ToString();
        }
    }
}
